package com.copera.cn.kaka.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

public class MyBarAndQrcodeUtils {
	private static final String TAG = "MyBarAndQrcodeUtils";

    public Bitmap overlayLogo(Bitmap b1, Bitmap b2) {

            float left = (b1.getHeight() - b2.getHeight()) / 2;
            float top = (b1.getWidth() - b2.getWidth()) / 2;

            Paint paint = new Paint();
            Canvas canvas = new Canvas(b1);

            canvas.drawBitmap(b2, left, top, paint);
            canvas.save(Canvas.ALL_SAVE_FLAG);
            canvas.restore();
           

            return b1;
    }

    // ��ɶ�ά�루Q/R Code����
    public Bitmap createTwoDCode(String content) throws WriterException {

            BitMatrix matrix = new MultiFormatWriter().encode(content,
                            BarcodeFormat.QR_CODE, 500, 500);
            int width = matrix.getWidth();
            int height = matrix.getHeight();

            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                            if (matrix.get(x, y)) {
                                    pixels[y * width + x] = 0xff000000;
                                    // �޸Ĵ�ֵ�ɸı��ά����ɫ��ʾ�����£�
                                    // pixels[y * width + x] = 0xff860C0C;
                            }
                    }
            }

            Bitmap bitmap = Bitmap.createBitmap(width, height,
                            Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

            return bitmap;
    }

    // ���һά���루barcode����
    public Bitmap createOneDCode(String content) throws WriterException {

            BitMatrix matrix = new MultiFormatWriter().encode(content,
                            BarcodeFormat.CODE_128, 260, 60);
            int width = matrix.getWidth();
            int height = matrix.getHeight();
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                            if (matrix.get(x, y)) {
                                    pixels[y * width + x] = 0xff000000;
                            }
                    }
            }

            Bitmap bitmap = Bitmap.createBitmap(width, height,
                            Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

            return bitmap;
    }

    // ����Bitmap������д����SD�����ļ��С�
    public void writeBitmapToSd(Bitmap bmp, String prefix, String name) {
            String filename = prefix + name + ".png";
            Log.d(TAG, name);

            //byte[] bm2ba = MySdcardUtils.bitmap2Bytes(bmp);

            //MySdcardUtils.writeBytes2File(bm2ba, filename);

    }

}
